package com.chuanyang.driverh.utils;

import android.app.Activity;
import android.app.ActivityManager;
import android.content.Context;
import android.content.pm.ApplicationInfo;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.os.Environment;
import android.telephony.TelephonyManager;
import android.text.TextUtils;
import android.util.DisplayMetrics;
import android.util.Log;
import android.view.Display;
import android.view.View;
import android.view.inputmethod.InputMethodManager;

import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.util.Iterator;
import java.util.List;


/**
 * 
 * JSAppUtil.java 提供一些android系统常用操作：如系统版本，SDK版本等
 * 
 * @author yanlu 2015-5-14
 */
public class JSAppUtil {
	/**
	 * 返回当前程序版本名
	 */
	public static PackageInfo getPackageInfo(Context context) {
		// String versionName = "1.0.0";
		PackageInfo pi = null;
		try {
			// ---get the package info---
			PackageManager pm = context.getPackageManager();
			// 这里的context.getPackageName()可以换成你要查看的程序的包名
			pi = pm.getPackageInfo(context.getPackageName(), 0);
			// versionName = pi.versionName;
			if (pi == null) {
				return null;
			}
		} catch (Exception e) {
			Log.e("VersionInfo", "Exception", e);
		}
		return pi;
	}

	// /**
	// * 退出应用
	// *
	// * @param context
	// */
	// public static void exitApp() {
	// List<Activity> list = JSBaseApplication.activityStack;
	// for (Activity ac : list) {
	// ac.finish();
	// }
	// list.clear();
	// System.gc();
	// }

	/**
	 * 返回当前程序应用名
	 */
	public static String getAppName(Context context) {
		PackageManager packageManager = null;
		ApplicationInfo applicationInfo = null;
		try {
			packageManager = context.getPackageManager();
			applicationInfo = packageManager.getApplicationInfo(context.getPackageName(), 0);
		} catch (PackageManager.NameNotFoundException e) {
		}
		String applicationName = (String) packageManager.getApplicationLabel(applicationInfo);
		return applicationName;
	}

	/**
	 * 返回运行进程名
	 */
	public static String getProcessAppName(Context context, int pID) {
		String processName = null;
		ActivityManager am = (ActivityManager) context.getSystemService(context.ACTIVITY_SERVICE);
		List l = am.getRunningAppProcesses();
		Iterator i = l.iterator();
		PackageManager pm = context.getPackageManager();
		while (i.hasNext()) {
			ActivityManager.RunningAppProcessInfo info = (ActivityManager.RunningAppProcessInfo) (i
					.next());
			try {
				if (info.pid == pID) {
					CharSequence c = pm.getApplicationLabel(pm.getApplicationInfo(info.processName,
							PackageManager.GET_META_DATA));
					// Log.d("Process", "Id: "+ info.pid +" ProcessName: "+
					// info.processName +"  Label: "+c.toString());
					// processName = c.toString();
					processName = info.processName;
					return processName;
				}
			} catch (Exception e) {
				// Log.d("Process", "Error>> :"+ e.toString());
			}
		}
		return processName;
	}


	/**
	 * 获取进程号对应的进程名
	 *
	 * @param pid 进程号
	 * @return 进程名
	 */
	public  static String getProcessName(int pid) {
		BufferedReader reader = null;
		try {
			reader = new BufferedReader(new FileReader("/proc/" + pid + "/cmdline"));
			String processName = reader.readLine();
			if (!TextUtils.isEmpty(processName)) {
				processName = processName.trim();
			}
			return processName;
		} catch (Throwable throwable) {
			throwable.printStackTrace();
		} finally {
			try {
				if (reader != null) {
					reader.close();
				}
			} catch (IOException exception) {
				exception.printStackTrace();
			}
		}
		return null;
	}

	/**
	 * 获得设备识别认证码
	 * 
	 * @return
	 */
	public static String getIMEI(Context context) {
		TelephonyManager tm = (TelephonyManager) context
				.getSystemService(Context.TELEPHONY_SERVICE);
		if (tm == null) {
			return "";
		}
		return tm.getDeviceId();
	}

	/**
	 * 关闭软键盘
	 * 
	 * @param context
	 */
	public static void closeKeyBox(Context context) {
		InputMethodManager imm = (InputMethodManager) context
				.getSystemService(Context.INPUT_METHOD_SERVICE);
		final View v = ((Activity) context).getWindow().peekDecorView();
		imm.hideSoftInputFromWindow(v.getWindowToken(), InputMethodManager.HIDE_NOT_ALWAYS);
	}

	/**
	 * 判断sd卡是否安装
	 * 
	 * @return
	 */
	public static boolean existSdcard() {
		if (Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)) {
			return true;
		} else {
			return false;
		}
	}

	/**
	 * 获得屏幕密度
	 * 
	 * @return
	 */
	public static float getDensity(Context context) {
		DisplayMetrics dm = context.getApplicationContext().getResources().getDisplayMetrics();
		return dm.density;
	}

	/**
	 * 获得屏幕尺寸
	 * 
	 * @param context
	 * @return
	 */
	public static final Display getScreenSize(Context context) {
		return ((Activity) context).getWindowManager().getDefaultDisplay();
	}

}
